home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / demos / panZoom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.9 KB  |  228 lines

  1. /* Copyright (c) Silicon Graphics, Inc. 1996 */
  2.  
  3. /* panZoom.c
  4.  *    This program takes the name of an image file in SGI .rgb 
  5.  *    format. It opens the file and creates a window displaying 
  6.  *    a subimage taken from the middle of the image.
  7.  *
  8.  *    The image can be panned by dragging the mouse with the
  9.  *    Left Mousebutton pressed.
  10.  * 
  11.  *    The subimage can be zoomed in or out using the middle and 
  12.  *    right mouse buttons, respectively.
  13.  *
  14.  *    Note that this program must be linked with -lrgbImageFile, 
  15.  *    which should be in the ../lib directory.
  16.  */
  17.  
  18. #include <GL/gl.h>
  19. #include <GL/glu.h>
  20. #include <GL/glut.h>
  21.  
  22. #include <math.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include "rgbImageFile.h"    /* should be in ../../include */
  26.  
  27. /*  Function Prototypes  */
  28.  
  29. GLvoid  initgfx( GLvoid );
  30. GLvoid  drawScene( GLvoid );
  31. GLvoid  reshape( GLsizei, GLsizei );
  32. GLvoid  keyboard( GLubyte, GLint, GLint );
  33. GLvoid  mouse( GLint, GLint, GLint, GLint );
  34. GLvoid  motion( GLint, GLint );
  35.  
  36. GLvoid    adjustSkipParams( GLvoid );
  37.  
  38. void printHelp( char * );
  39.  
  40. /* Global Definitions */
  41.  
  42. #define KEY_ESC    27    /* ascii value for the escape key */
  43.  
  44. /* Global Variables */
  45.  
  46. GLuint        *image;
  47. GLint        skipRows, skipPixels;
  48. GLint        xStart, yStart;
  49.  
  50. GLint        imageWidth, imageHeight;
  51. GLint        subimageWidth, subimageHeight;
  52.  
  53. GLboolean    panning = GL_FALSE;
  54.  
  55. GLfloat        zoomFactor = 1.0;
  56.  
  57. GLvoid
  58. main ( int argc, char *argv[])
  59. {
  60.     GLsizei width, height;
  61.  
  62.     glutInit( &argc, argv );
  63.  
  64.     if (argc < 2) {
  65.         fprintf (stderr, "usage: %s <imageFileName>\n", 
  66.             argv[0] );
  67.         exit (1);
  68.     }
  69.     
  70.     image = rgbReadImageFile(argv[1], &imageWidth, &imageHeight);
  71.  
  72.     /* set up the window size and the sub-image size 
  73.      * to be 1/4 of the original image size.
  74.      */
  75.     subimageWidth = imageWidth/2;
  76.     subimageHeight = imageHeight/2;
  77.  
  78.     glutInitWindowPosition( 100, 100 );
  79.     glutInitWindowSize( subimageWidth, subimageHeight );
  80.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
  81.     glutCreateWindow( argv[0] );
  82.  
  83.     initgfx();
  84.  
  85.     glutMouseFunc( mouse );
  86.     glutMotionFunc( motion );
  87.     glutKeyboardFunc( keyboard );
  88.     glutReshapeFunc( reshape );
  89.     glutDisplayFunc( drawScene ); 
  90.  
  91.     printHelp( argv[0] );
  92.  
  93.     glutMainLoop();
  94. }
  95.  
  96. GLvoid
  97. printHelp( char *progname )
  98. {
  99.     fprintf(stdout, 
  100.         "\n%s, a program to pan and Zoom an image\n\n"\
  101.         "Left Mousebutton, down        - pan image\n\n"
  102.         "<z> key            - zoom in on subimage\n"
  103.         "<Z> key            - zoom out on subimage\n"
  104.         "Escape key            - exit the program\n\n",
  105.         progname);
  106. }
  107.  
  108. GLvoid
  109. initgfx( void )
  110. {
  111.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  112. }
  113.  
  114. GLvoid 
  115. keyboard( GLubyte key, GLint x, GLint y )
  116. {
  117.     switch (key) {
  118.     case 'z':    /* increase zoom factor -- zoom in */
  119.         zoomFactor *= 2;
  120.         break;
  121.     case 'Z':    /* decrease zoom factor -- zoom out */
  122.         zoomFactor /= 2;
  123.         break;
  124.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  125.         exit(0);
  126.     }
  127.     printf("Zoom Factor = %g\n", zoomFactor);
  128.     glutPostRedisplay();
  129. }
  130.  
  131. GLvoid
  132. reshape( GLsizei width, GLsizei height )
  133. {
  134.     glViewport( 0, 0, width, height);
  135.  
  136.     glMatrixMode( GL_PROJECTION );
  137.     glLoadIdentity();
  138.     gluOrtho2D( 0.0, (GLdouble) width, 0.0, (GLdouble) height );
  139.     glMatrixMode( GL_MODELVIEW );
  140.     glLoadIdentity();
  141.     glTranslatef( 0.375, 0.375, 0.0 );
  142.  
  143.     subimageWidth = width;
  144.     subimageHeight = height;
  145.     if (subimageWidth > imageWidth)
  146.         subimageWidth = imageWidth;    
  147.     if (subimageHeight > imageHeight)
  148.         subimageHeight = imageHeight;    
  149. }
  150.  
  151. GLvoid 
  152. mouse( GLint button, GLint state, GLint x, GLint y )
  153. {
  154.     switch (button) {
  155.     case GLUT_LEFT_BUTTON:
  156.         if (state == GLUT_DOWN) {
  157.             xStart = x; /* save current mouse position */
  158.             yStart = y;
  159.             panning = GL_TRUE;
  160.         } else {
  161.             panning = GL_FALSE;
  162.         }
  163.         break;
  164.     }
  165. }
  166.  
  167. GLvoid
  168. motion( int x, int y )
  169. {
  170.     if (panning) {
  171.         /* adjust skip info based on how far the mouse moved */
  172.         skipPixels -= (x - xStart);
  173.         skipRows += (y - yStart);
  174.  
  175.         
  176.         xStart = x;    /* Update the stored mouse position */
  177.         yStart = y;
  178.  
  179.         glutPostRedisplay();
  180.     }
  181. }
  182.  
  183. /* make sure the selected rectangle is always
  184.  * completely inside the original image    
  185.  */
  186. GLvoid
  187. adjustSkipParams( GLvoid )
  188. {            
  189.     if (skipPixels < 0)
  190.         skipPixels = 0;
  191.     if (skipPixels > imageWidth-subimageWidth)
  192.         skipPixels = imageWidth-subimageWidth;
  193.  
  194.     if (skipRows < 0)
  195.         skipRows = 0;
  196.     if (skipRows >= imageHeight-subimageHeight)
  197.         skipRows = imageHeight-subimageHeight;
  198.  
  199. GLvoid
  200. drawScene( GLvoid )
  201. {
  202.     glClear( GL_COLOR_BUFFER_BIT );
  203.  
  204.     adjustSkipParams ();
  205.  
  206.     /* Set up to grab a portion of the original stored image. */
  207.  
  208.     /* Set the pixel width of the original image. */
  209.     glPixelStorei( GL_UNPACK_ROW_LENGTH, imageWidth );
  210.  
  211.     /* Indicate how many pixels to skip in the x and y directions
  212.      * (from the lower left corner of the original image) */
  213.     glPixelStorei( GL_UNPACK_SKIP_PIXELS, skipPixels );
  214.     glPixelStorei( GL_UNPACK_SKIP_ROWS, skipRows );
  215.  
  216.     /* In this example, the xy coordinates to map directly 
  217.      * to window coordinates.  This would not be the case if any
  218.      * projection or modeling transforms had been applied.
  219.      */
  220.     glRasterPos2i( 0,  0 );
  221.     glPixelZoom( zoomFactor, zoomFactor );
  222.     glDrawPixels (subimageWidth, subimageHeight, GL_RGBA,
  223.              GL_UNSIGNED_BYTE, image);
  224.  
  225.     glutSwapBuffers();
  226. }
  227.